﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;

namespace HospitalSystem
{
    public partial class plan : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        string CID;
        string FirstName;
        string LastName;
        string caseNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected void CaseSelector3_CheckedChanged(object sender, EventArgs e)
        {


            foreach (GridViewRow row in GridView2.Rows)
            {
                RadioButton cb = (RadioButton)row.FindControl("CaseSelector3");
                if (cb != null && cb.Checked)
                {
                    CID = GridView2.Rows[row.RowIndex].Cells[0].Text;
                    FirstName = GridView2.Rows[row.RowIndex].Cells[1].Text;
                    LastName = GridView2.Rows[row.RowIndex].Cells[2].Text;
                }
            }

            var query = from a in db.Evaluations
                        where a.CID.Equals(CID)
                        select a.CaseNo;

            if (query.Any())
            {
                foreach (var q in query)
                {
                    caseNumber = q.ToString();
                }
                ////Memory Session Valuable
                Session["CID"] = CID;
                Session["FirstName"] = FirstName;
                Session["LastName"] = LastName;
                Session["CaseNo"] = caseNumber;
                ////Redirect
                Response.Redirect("evaluationscreening.aspx");
            }
            else
            {
                Session["CID"] = CID;
                Session["FirstName"] = FirstName;
                Session["LastName"] = LastName;
                MessageBox2("เด็กรายได้ยังไม่ได้ทำการคัดกรองเราจะพาคุณไปหน้าทำการคัดกรอง", "cal.aspx");
                GridView2.DataSource = null;
                GridView2.DataBind();

            }
               
        }

        private void MessageBox2(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            var query = from a in db.Persons
                        where (TextBox1.Text.Equals(a.CID)) || (TextBox1.Text.Equals(a.FirstName)) || (TextBox1.Text.Equals(a.LastName))
                        select new
                        {
                            a.CID,
                            a.FirstName,
                            a.LastName,
                            a.Gender
                        };
            if (query.FirstOrDefault() == null)
            {
                Label1.Text = "ไม่มีเลขบัตรประชาชนที่ค้นหา หรือยังไม่เคยคัดกรอง";
                Label1.ForeColor = Color.Red;
            }
            else
            {
                Label1.Text = "";
                GridView2.DataSource = query;
                GridView2.DataBind();
            }
        }
    }
}